package com.ruoyi.topic.controller;

import java.util.List;
import java.util.UUID;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.topic.domain.TbTopic;
import com.ruoyi.topic.service.ITbTopicService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 学习主题，学习板块Controller
 * 
 * @author 劳雅玲
 * @date 2024-03-01
 */
@RestController
@RequestMapping("/topic/topic")
public class TbTopicController extends BaseController
{
    @Autowired
    private ITbTopicService tbTopicService;

    /**
     * 查询学习主题，学习板块列表
     */
    @PreAuthorize("@ss.hasPermi('topic:topic:list')")
    @GetMapping("/list")
    public TableDataInfo list(TbTopic tbTopic)
    {
        startPage();
        List<TbTopic> list = tbTopicService.selectTbTopicList(tbTopic);
        return getDataTable(list);
    }

    /**
     * 导出学习主题，学习板块列表
     */
    @PreAuthorize("@ss.hasPermi('topic:topic:export')")
    @Log(title = "学习主题，学习板块", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TbTopic tbTopic)
    {
        List<TbTopic> list = tbTopicService.selectTbTopicList(tbTopic);
        ExcelUtil<TbTopic> util = new ExcelUtil<TbTopic>(TbTopic.class);
        util.exportExcel(response, list, "学习主题，学习板块数据");
    }

    /**
     * 获取学习主题，学习板块详细信息
     */
    @PreAuthorize("@ss.hasPermi('topic:topic:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id)
    {
        return success(tbTopicService.selectTbTopicById(id));
    }

    /**
     * 新增学习主题，学习板块
     */
    @PreAuthorize("@ss.hasPermi('topic:topic:add')")
    @Log(title = "学习主题，学习板块", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TbTopic tbTopic)
    {
        tbTopic.setTopicId(UUID.randomUUID().toString().replace("-","")); //生成一个随机字符串作为topic id
        return toAjax(tbTopicService.insertTbTopic(tbTopic));
    }

    /**
     * 修改学习主题，学习板块
     */
    @PreAuthorize("@ss.hasPermi('topic:topic:edit')")
    @Log(title = "学习主题，学习板块", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TbTopic tbTopic)
    {
        return toAjax(tbTopicService.updateTbTopic(tbTopic));
    }

    /**
     * 删除学习主题，学习板块
     */
    @PreAuthorize("@ss.hasPermi('topic:topic:remove')")
    @Log(title = "学习主题，学习板块", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids)
    {
        return toAjax(tbTopicService.deleteTbTopicByIds(ids));
    }
}
